package hku.cs.lunuo.rank;

import hku.cs.lunuo.Global;
import hku.cs.lunuo.Global.Singleton;
import hku.cs.lunuo.cost.Cost;
import hku.cs.lunuo.tools.MD5;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class RankMain {
	static SAXBuilder sbuilder = new SAXBuilder();
	List<File> srcDirs = null;
	File models = null;
	File err = null;

	public RankMain(Singleton s) {
	}

	public static RankMain v() {
		return Global.v()._RankMain();
	}

	public static void main(String[] args) {
		List<File> src_dirs = new ArrayList<File>();
		String process_dir = "";
		File models = null;
		File err = null;
		for (int i = 0; i < args.length; i++) {
			if (args[i].equals("-process-dir"))
				process_dir = args[++i];
			else if (args[i].equals("-source-dir"))
				src_dirs.add(new File(args[++i]));
			else if (args[i].equals("-models"))
				models = new File(args[++i]);
			else if (args[i].equals("-err"))
				err = new File(args[++i]);
		}

		RankMain rm = RankMain.v();
		rm.err = err;
		rm.models = models;
		rm.srcDirs = src_dirs;// new RankMain(src_dirs, models, err);
		File f = new File(process_dir);

		try {
			rm.doMain(f);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void doMain(File testDir) throws JDOMException, IOException,
			ClassNotFoundException, InstantiationException,
			IllegalAccessException {
		File[] testcaseDirs = testDir.listFiles();
		if (testcaseDirs == null) {
			System.out.println("Your Directory " + testDir.getName()
					+ " is Not exit or is File but not Directory");
			return;
		}
		for (File f : testcaseDirs) {
			if (f.isDirectory()) {
//				System.out.println(f.getName());
				File resultF = new File(f, "result.xml");
				if (!resultF.exists()) {
					continue;
				}
				Document xmlDoc = sbuilder.build(resultF);
				Element xmlRoot = xmlDoc.getRootElement();
				if (!Boolean.valueOf(xmlRoot.getAttributeValue("status"))) {
					Iterator xmlIt = xmlRoot.getChildren().iterator();
					List<String> argLst = new ArrayList<String>();
					while (xmlIt.hasNext()) {
						Element e = (Element) xmlIt.next();
						argLst.add(e.getAttributeValue("name"));
					}
					StringBuffer sbu = new StringBuffer();
					for (String s : argLst) {
						sbu.append(s);
					}

					String ssnu = MD5.getMD5(sbu.toString().getBytes());

					SAXBuilder sb = new SAXBuilder();
					Document xmlDocx = sb.build(models);
					Element xmlRootx = xmlDocx.getRootElement();
					Iterator xmlModelIt = xmlRootx.getChildren("model")
							.iterator();

					while (xmlModelIt.hasNext()) {
						Element xmlModel = (Element) xmlModelIt.next();
						String klassName = xmlModel.getChildText("class");
						Class cl = Class.forName(klassName);
						Global.v().model = (Model) cl.newInstance();
						String voter = xmlModel.getChildText("voter");
						cl = Class.forName(voter);
						Global.v().exceptionFilter = true;
						Global.v().voter = (Voter) cl.newInstance();
						Global.v().toDoDir = f;
						Global.v().modelName = xmlModel.getAttributeValue("name");
						Model model = Global.v().model;
						
						model.prepaerProfile(f, argLst);
						model.analyze();
						model.toStmt();
						model.rank();
						model.toXML(new File(f, xmlModel
								.getAttributeValue("name")
								+ ".xml"));
//						new Expense(err).getExpense(Global.v().result);
						new Cost(err).getExpense(Global.v().result);
						Global.v().result.data.clear();
					}
					System.out.println();
//					XComparor.main(new String[] {new File(f, "Jaccard.xml").getAbsolutePath(),
//							new File(f, "JKBC.xml").getAbsolutePath()});
				}
			}
		}
	}
}
